﻿using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV001.Domain.Entity;
using WebApiV001.Implement.Data;
using WebApiV001.Interface;

namespace WebApiV001.Domain
{
    public class DbInitializer
    {
        public static void Seed(IServiceProvider service)
        {
            using (var scope  = service.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin3200DbContext)) as Admin3200DbContext;

                db.Database.EnsureCreated();

                var hasUser = db.Users.Any();

                if (!hasUser)
                {
                    var userRes = scope.ServiceProvider.GetService(typeof(IRepository<Users>)) as EfRespository<Users>;
                    var roleRes = scope.ServiceProvider.GetService(typeof(IRepository<Roles>)) as EfRespository<Roles>;
                    var usersRoles = scope.ServiceProvider.GetService(typeof(IRepository<UsersRoles>)) as EfRespository<UsersRoles>;


                    var roles = new Roles
                    {
                        RoleName = "管理员",
                        Descripiton = "这是一个拥有生杀予夺的管理员"
                    };

                    roleRes.Add(roles);

                    var users = new Users
                    {
                        Username = "小俊",
                        Password = "123456",
                    };

                    userRes.Add(users);

                    usersRoles.Add(new UsersRoles 
                    {
                        UsersId = users.Id,
                        RolesId = roles.Id
                    });

                }
            }
        }
    }
}
